package com.zxl.yjb.activity.nearby;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.zxl.yjb.R;
import com.zxl.yjb.activity.home.order.SosDetailActivity;
import com.zxl.yjb.adapter.NearbyHelpListAdapter;
import com.zxl.yjb.app.Const;
import com.zxl.yjb.base.BaseActivity;
import com.zxl.yjb.bean.LoginSucessBean;
import com.zxl.yjb.bean.NearbyHelpBean;
import com.zxl.yjb.http.manager.DataManager;
import com.zxl.yjb.http.observer.ObserverOnNextListener;
import com.zxl.yjb.http.observer.ProgressObserver;
import com.zxl.yjb.utils.DisplayUtil;
import com.zxl.yjb.utils.SaveBeanUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class NearbyHistoryActivity extends BaseActivity {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_page_title)
    TextView tvPageTitle;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;

    private DataManager dataManager;
    private LoginSucessBean loginSucessBean;
    private int currentPager = 1;
    private int pageSize = 30;
    private NearbyHelpListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_his_sos);
        loginSucessBean = (LoginSucessBean) SaveBeanUtil.getBean(this, Const.VIPINFO);
        ButterKnife.bind(this);
        initView();
    }

    @Override
    protected void initView() {
        tvPageTitle.setText("历史求助");
        setAdapter();
        getSOSList();
    }

    private void setAdapter() {
        adapter = new NearbyHelpListAdapter(R.layout.nearbyhelp_item);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                NearbyHelpBean item = (NearbyHelpBean) adapter.getItem(position);
                startActivity(new Intent(NearbyHistoryActivity.this, SosDetailActivity.class)
                        .putExtra("id", item.getId() + ""));
            }
        });
        adapter.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(final BaseQuickAdapter adapter, View view, final int position) {
                int uid = ((NearbyHelpBean) adapter.getItem(position)).getUid();
                if (uid == loginSucessBean.getUid()) {
                    DisplayUtil.diaplayDialog(NearbyHistoryActivity.this, "提示", "确定要删除此信息吗？", "取消", "删除", null, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            del(((NearbyHelpBean) adapter.getItem(position)).getId() + "", position);
                        }
                    });
                }

                return true;
            }
        });
        //创建布局管理
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerview.setLayoutManager(layoutManager);
        recyclerview.setAdapter(adapter);

    }

    private void del(String id, final int position) {
        dataManager = new DataManager();
        Map<String, String> map = new HashMap<>();
        map.put("id", id);
        ObserverOnNextListener<Object> listener = new ObserverOnNextListener<Object>() {
            @Override
            public void onNext(Object o) {
                adapter.remove(position);
                adapter.notifyDataSetChanged();
            }
        };
        dataManager.sosdel(new ProgressObserver<Object>(listener, NearbyHistoryActivity.this), map);
    }

    private void getSOSList() {
        Map<String, String> map = new HashMap<>();
        map.put("page", currentPager + "");
        map.put("count", pageSize + "");
        dataManager = new DataManager();
        ObserverOnNextListener<List<NearbyHelpBean>> listener = new ObserverOnNextListener<List<NearbyHelpBean>>() {
            @Override
            public void onNext(List<NearbyHelpBean> detailBeanList) {
                if (currentPager++ == 1) {
                    adapter.setNewData(detailBeanList);
                    refreshLayout.finishRefresh();
                } else {
                    adapter.addData(detailBeanList);
                    refreshLayout.finishLoadMore();
                    if (detailBeanList.size() < pageSize) {
                        refreshLayout.finishLoadMoreWithNoMoreData();//将不会再次触发加载更多事件
                    }
                }
            }
        };
        dataManager.soslist_my(new ProgressObserver<List<NearbyHelpBean>>(listener, this) {
            @Override
            public void showDialog() {
            }
        }, map);
    }



    @OnClick(R.id.iv_back)
    public void onViewClicked() {

        finish();
    }
}
